package hzn.distributeLock;

import java.util.Random;

public class Seller {
    int num=10;
    void sell(){
        if(num>=0){
            System.out.println(Thread.currentThread().getName()+"出售票："+num);
            try{
                int time=new Random().nextInt(5000);
                Thread.sleep(time);
            }catch (Exception e){}
            num--;
        }
    }

    public void sellTicketWithLock() throws Exception {
        MyLock lock=new MyLock();
        //1.得到锁
        lock.acquireLock();
        //2.开始卖票
        sell();
        //3.释放锁
        lock.releaseLock();
    }

    public static void main(String[] args) {
            Seller seller=new Seller();
            for(int i=0;i<10;i++){
                new Thread("窗口"+i){
                    @Override
                    public void run() {
                        try {
                            seller.sellTicketWithLock();
                        }catch (Exception e){}
                    }
                }.start();
            }
    }
}
